package org.raymond.iworks.study.basic.feature;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Stream;

public class FuncBreakUpTest {

    /*
    public Stream<OrderDto> getOrderByUser(String userId){
        return orderRepo.findOrderByUser().stream()
                .map(order-> {
                    OrderDto dto = new OrderDto();
                    dto.setOrderId(order.getOrderId());
                    dto.setTitle(order.getTitle().split("#")[0]);
                    dto.setCreateDate(order.getCreateDate().getTime());
                    return dto;
                });
    }*/

    // 改进一: 可以尝试把dto的创建过程给独立开来.
    // 因为转换动作不是主要的业务逻辑,我们通常不会关心其中到底发生了啥
    /*
    public Stream<OrderDto> getOrderByUser(String userId){
        return orderRepo.findOrderByUser().stream()
                .map(this::toOrderDto);
    }
    public OrderDto toOrderDto(Order order){
        OrderDto dto = new OrderDto();
        dto.setOrderId(order.getOrderId());
        dto.setTitle(order.getTitle().split("#")[0]);
        dto.setCreateDate(order.getCreateDate().getTime());
        return dto;
    }*/

    // 改进二: 如果OrderDto的构造函数,参数就是Order: public OrderDto(Order order),
    // 那我们就可以把整个转换逻辑从主逻辑中移除出去,整个代码就可以非常的清爽
    /*
    public Stream<OrderDto> getOrderByUser(String userId){
        return orderRepo.findOrderByUser().stream()
                .map(OrderDto::new);
    }*/

    // filter可以使用Predicate去代替, registarIsCorrect, 就可以当作filter的参数
    /*
    Predicate<Registar> registarIsCorrect = reg ->
    reg.getRegulationId() != null
    && reg.getRegulationId() != 0
    && reg.getType() == 0;
     */
    @Test
    public void toDTO(){
        Student s1 = new Student("raymond", 35, "male", 100);
        Student s2 = new Student("alva", 18, "female", 85.5f);
        Student s3 = new Student("alice", 19, "female", 91.5f);
        Student s4 = new Student("ann", 18, "female", 90);

        List<StudentDTO> s = List.of(s1, s2, s3, s4).stream().map(StudentDTO::new).toList();
        System.out.println(s.get(0));
    }
}


record StudentDTO(Student student){
    // StudentDTO(){} // 不能声明构造函数
    // String x; // 不能声明实例属性
    private static String y; // 可以声明类属性
}
